//#include "../include/main.h
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include "../include/filedesc.h"
#include "../include/hash.h"



/*Hash Function*/

int hash(char *file_name)
{
        char first_char;
        int hash_value =-1;      
        first_char = file_name[0];
        if(first_char >= 'A' && first_char <= 'Z')
        hash_value=first_char%65 ;
       else if (first_char >= 'a' && first_char <= 'z')
                hash_value=(first_char%97)+26;
        else
                 hash_value =-1;     //File name Validation needs to be done
        return hash_value;
}

int display()
{
        int i=0;
        struct list *temp;
        for(i=0;i<51;i++)
        {
                while(hash_table[i]!=NULL)
                {      
                        temp = hash_table[i];
                        while(temp!=NULL)
                        {
                                printf("%s\t%s", temp->file_name,temp->path_name);
                                temp=temp->next;
                               
                        }
                        printf("\n");
                        i++;
                        if(i==51)
                                break;
                }
        }
                return 1;
}

/*Used for search if the whole file name is given:
  Takes the hash key value and file name to searched for as input and assigns that key address location
  to the start pointer of hash Bucket and start search for the given file from that start location */

 list* src_file(int index, char *name_File)
{      
       list *start,*first,*temp,*head;
        head=(list *)malloc(sizeof(list));
        temp=head;
      start=hash_table[index];  
        while(start!=NULL)
        {       if (strcmp(start->file_name,name_File)==0)
                {      
                        first= ( list *)malloc(sizeof( list));
                strcpy(first->path_name,start->path_name);  
                strcpy(first->file_name,start->file_name);                
                first->next=NULL;
                temp->next=first;
                temp=first;                      
                }
                start = start->next;
        }
        return head->next;
}
/*This function will take the file name and calculates the hash value based on the first character in file name
and and calls the src_file function. Returns the FileDescriptor pointer */

 list* search_start_point(char *name_File)
{       int index;
         list *temp;
        temp=(list *)malloc(sizeof(list));
         index = hash(name_File);
        if(hash_table[index] == NULL)
        {
               // printf("0 Files Found. Search Ended...\n");    
                return NULL;
        }
        else if(index == -1)
        {
               // printf("Invalid file name\n");
                return NULL;
        }
        else{
                temp=src_file(index,name_File);
               
        }
          return temp;
}

/*Used for search if the prefix of file name is given:
 calculates the hash key value based on the first character given and prefix of file name to searched
 for as input and assigns that key address location to the start pointer of hash Bucket and start search
 for the given file from that start location.Returns the FileDescriptor pointer*/

 list* search_files(char *startchars)
{       int index,length;
        list *start,*first,*temp,*head;
        head=(list *)malloc(sizeof( list));
head->next=NULL;
        temp=head;
        index = hash(startchars);
        start=hash_table[index];
        length=strlen(startchars);
        if(hash_table[index] == NULL)
        {
                return NULL;
        }
        else if(index == -1)
        {
                return NULL;
        }
        else{
                        while(start!=NULL)
                {      
                        if (strncmp(start->file_name,startchars,length)==0)
                           {    
                                first= ( list *)malloc(sizeof( list));
                                strcpy(first->path_name,start->path_name);  
                                strcpy(first->file_name,start->file_name);                
                                first->next=NULL;
                                temp->next=first;
                                temp=first;              
                           }
                        start = start->next;
                }
               
        }
          return head->next;
}
/*Inserting the node into the hash table in sorted order */

int insert_into_list( char file[],char path[])
{
        int hash_value=hash(file);
        list *node;
        node = ( list *)malloc(sizeof( list));
//Store the file name and Fullpath in the node... and inserting in Sorted order
        strcpy(node->file_name, file);
       // printf("node %s\t",node->file_name);
        strcpy(node->path_name, path);
         //printf("node %s\t",node->path_name);
        node->next = NULL;

        if(hash_table[hash_value]== NULL)
        {

                hash_table[hash_value]= node;
                return 1;
        }
        //insertion at end
        /*else
        {
                list *temp;
                temp = hash_table[hash_value];

                while(temp->next!=NULL)
                        temp = temp->next;

                temp->next = node;      
        }*/
                //inertion at beginning

                /*else
        {      
                node->next=hash_table[hash_value];
                hash_table[hash_value]=node;                                    
                return 1;
        }*/
               
        //insertion in sorted order
        else if(strcmp((hash_table[hash_value]->file_name),(node->file_name)) > 0)
        {      
                node->next=hash_table[hash_value];
                hash_table[hash_value]=node;
                return 1;
        }

                else
        {
                list *prev,*curr;
                curr=hash_table[hash_value];
                prev =hash_table[hash_value];
               
                while(curr!=NULL&&node != NULL)
                {                      
                        if(strcmp((curr->file_name),(node->file_name)) > 0)
                                break;
                        prev = curr;
                        curr = curr->next;
                }
                node->next = prev->next;
                prev->next = node;
                return 1;
        }      
}


int srcFilePath( char *file_name,char *file_path)
{	
        int i=0,l,n=strlen(file_path),j,c,count=0;
        char *name,*fname;
         char only_fname[200],path[200];
         char **tokens;
	struct list *temp=NULL;
        
	

for(i=0;i<51;i++)
	{
		while(hash_table[i]!=NULL)
		{	
			temp = hash_table[i];
			while(temp!=NULL)
			{
                              
				int x=(strcmp(file_path, temp->path_name) && strcmp(file_name, temp->file_name));
                               if(x!=0)
				temp=temp->next;
				else {
                                         printf("File found in the gn path");
                                         printf("path in hash:%s\n\n",temp->path_name);
                                         printf("path in hash:%s\n\n",temp->file_name);
					return 1;
					//break;
					}
			}
			i++;
			if(i==51)
                                break;
		}
	}

     return 0;
}

/*Deleting a node from the hash table based on the file name and path name*/
int deletion(char *f,char *p)
{       int hash_value;
        hash_value=hash(f);
        int flag=0;
        list *temp1, *temp2,*del;
        temp1 = hash_table[hash_value];
        temp2 = hash_table[hash_value];
       
         if(temp1==NULL)
                {
                 // printf("no entry to delete");
                  return 0;
                }

        else if(strcmp(temp1->file_name,f)==0 && strcmp(temp1->path_name,p)==0)
        {
               // printf("%s  is deleted\n\n",f);
                flag=1;
                temp2= temp1->next;
                hash_table[hash_value]=temp2;
                //free(temp1);
                return 1;
        }
        else
        {                  
                while(temp1!= NULL)
                {      
                        if(strcmp(temp1->file_name,f)==0 && strcmp(temp1->path_name,p)==0)
                        {     //printf("%s is deleted\n",f);
                                {      
                                        temp2->next=temp1->next;
                                        del=temp1;
                                        temp1=temp1->next;
                                        free(del);
                                        flag=1;
                                }
                        }
                        else
                        {
                                temp2 = temp1;                                  
                                temp1 = temp1->next;
                               
                        }
                 }          
                        return 1;
        }
}


